package cn.ffcs.bpm.service;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import java.util.List;

import cn.ffcs.base.dao.BaseDAO;
import cn.ffcs.base.service.BaseService;
import cn.ffcs.base.web.PageControl;
import cn.ffcs.common.dao.AttachDAO;
import cn.ffcs.common.model.Attach;
import cn.ffcs.bpm.model.ProcessConfig;
import cn.ffcs.bpm.model.ProcessConfigDetail;

/**
 * 流程配置服务层<br/>
 * <table>
 * <tr>
 * <td>负责人/principal:</td>
 * <td colspan="2">pany</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author pany
 * @version $Id$
 */
public class ProcessConfigService<D extends ProcessConfig> extends BaseService<D>
implements IProcessConfigService<D>
{
    private ProcessConfigDetailService processConfigDetailService;

    public void saveOrUpdate(D processConfig){
        if(processConfig.getCreateTime()==null||"".equals(processConfig.getCreateTime())){
            processConfig.setCreateTime(super.getCurrSysdate());
        }
        processConfig.setLastModifyTime(super.getCurrSysdate());
        if(processConfig==null||processConfig.getId()==null||"".equals(processConfig.getId())){
            processConfig.setRefEntity("cn.ffcs.businessrequest.model.BusinessRequest");
            super.saveOrUpdate(processConfig);
            //新增配置时，自动生成开始节点和结束节点
            ProcessConfigDetail endDetail=new ProcessConfigDetail();
            endDetail.setName("结束");
            endDetail.setNodeType("1");
            endDetail.setProcessConfig(processConfig);
            processConfigDetailService.saveOrUpdate(endDetail);
            ProcessConfigDetail startDetail=new ProcessConfigDetail();
            startDetail.setName("开始");
            startDetail.setNodeType("0");
            startDetail.setProcessConfig(processConfig);
            processConfigDetailService.saveOrUpdate(startDetail,endDetail.getId()+"");
        }else{
            super.saveOrUpdate(processConfig);
        }
    }
    
    /**
     * 删除流程配置实例
     * @param b
     * @return
     */
    public void remove(D processConfig)
    {
        super.remove(processConfig);
        //删除流程配置，同时删除流程对应配置和节点所对应的下一个节点
        Criterion cond = null;
        cond = Restrictions.eq("processConfig", processConfig);
        // 获得已经设置的下一节点
        List<ProcessConfigDetail> processConfigDetails = processConfigDetailService.findBy(cond);
        for( ProcessConfigDetail detail:processConfigDetails){
            processConfigDetailService.remove(detail);
        }
    }
    
    /**
     * 删除流程配置实例
     * @param b
     * @return
     */
    public void batchDel(String[] ids)
    {
        if (ids != null && ids.length > 0)
        {
            for (String id : ids)
            {
                try{
                    ProcessConfig processConfig=super.findById(new Integer(id));
                    remove((D)processConfig);
                }catch(Exception e){
                    continue;
                }
            }
        }
    }
 
    
    
    public ProcessConfigDetailService getProcessConfigDetailService()
    {
        return processConfigDetailService;
    }

    public void setProcessConfigDetailService(
            ProcessConfigDetailService processConfigDetailService)
    {
        this.processConfigDetailService = processConfigDetailService;
    }
    

    

    
}
